import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
import { getToken } from "@/utils/auth"; // get token from cookie
import getPageTitle from "@/utils/get-page-title";
import { datas0 } from "@/router/admin";
import { datas2 } from "@/router/super";
import { datas1 } from "@/router/member";
// import { datas2, dataes2 } from "@/router/gradeTwo";

//判断路由
const addMethods = (router, role) => {
  // let data2;
  // if (store.state.user.info.locat == "2") {
  //   data2 = datas2;
  // } else {
  //   data2 = dataes2;
  // }
  // data2.forEach((els) => {
  //   router.options.routes.push(els);
  // });
  // router.addRoutes(datas2);
  if (role == "1") {
    datas0.forEach((ele) => {
      router.options.routes.push(ele);
    });
    router.addRoutes(datas0);
  } else if (role == "2") {
    datas2.forEach((ele) => {
      router.options.routes.push(ele);
    });
    router.addRoutes(datas2);
  } else {
    console.log(datas1);
    // router.options.routes.push({
    //   path: "*",
    //   redirect: "/404",
    //   hidden: true,
    // });
    datas1.forEach((ele) => {
      router.options.routes.push(ele);
    });
    router.addRoutes(datas1);
  }
};

NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = ["/login"]; // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  console.log(to, "to");
  console.log(store.state.user.info, "info");
  if (store.state.user.info) {
  }
  // start progress bar
  NProgress.start();

  // set page title
  document.title = getPageTitle(to.meta.title);

  // determine whether the user has logged in
  const hasToken = getToken();
  const arrs = ["/web", "/web/contract"];
  if (hasToken) {
    if (to.path === "/login") {
      // if is logged in, redirect to the home page
      next("/");
      NProgress.done();
    } else {
      const hasGetUserInfo = store.getters.name;
      console.log(hasGetUserInfo);
      if (hasGetUserInfo) {
        next();
      } else {
        try {
          // get user info
          await store.dispatch("user/getInfo");
          console.log(store.state.user.info, "info");
          // if (store.state.user.info.locat == "1") {
          //   let data1 = datas1;
          //   data1.forEach((els) => {
          //     router.options.routes.push(els);
          //   });
          //   router.addRoutes(datas1);
          // } else if (store.state.user.info.dept == "3") {
          //   let data2 = datas2;
          //   data2.forEach((els) => {
          //     router.options.routes.push(els);
          //   });
          //   router.addRoutes(datas2);
          //   addMethods(router);
          // } else {
          //   router.options.routes.push({
          //     path: "*",
          //     redirect: "/404",
          //     hidden: true,
          //   });
          // }
          await addMethods(router, store.state.user.info.role);
          next({ ...to, replace: true });
        } catch (error) {
          // remove token and go to login page to re-login
          await store.dispatch("user/resetToken");
          Message.error(error || "Has Error");
          next(`/login?redirect=${to.path}`);
          NProgress.done();
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next();
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      // next(`/login?redirect=${to.path}`)
      // console.log('5555')
      // next('/web')
      // 项目启动首先进入官网
      if (arrs.includes(to.path)) {
        next();
      }
      // 通过网址栏去登录页
      else if (to.path == "/login") {
        next(`/login?redirect=${to.path}`);
      } else {
        next("/login");
      }
      NProgress.done();
    }
  }
});

router.afterEach(() => {
  // finish progress bar
  NProgress.done();
});
